﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;
using Telerik.Web.UI;

namespace HCFramework.Product.CategoryManage
{
    public partial class CategoryManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ChooseProductCategory1.LoadTreeData();
                ChooseProductCategory1.SelectedCategoryID = M.UrlQuery.parentID;
            }
        }
        protected void RadGridMain_PreRender(object sender, EventArgs e)
        {
            LoadGridData();
        }
        public void LoadGridData()
        {
            HCDataContext db = new HCDataContext();
            var result = from a in db.ProductCategories
                         where M.UrlQuery.parentID.HasValue ? a.ParentID == M.UrlQuery.parentID: a.ParentID == null
                         select a;
            result = result.OrderBy(ik => ik.OrderDisplay);

            RadGridMain.DataSource = result;
            RadGridMain.DataBind();
        }
        protected void RadGridMain_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Them")
            {
                TextBox txtName = e.Item.FindControl("txtName") as TextBox;
                TextBox txtOrderDisplay = e.Item.FindControl("txtOrderDisplay") as TextBox;
                CheckBox chkIsActive = e.Item.FindControl("chkIsActive") as CheckBox;

                ProductCategory cat = new ProductCategory();
                cat.IsActive = chkIsActive.Checked;
                cat.Name = txtName.Text;
                cat.OrderDisplay = int.Parse(txtOrderDisplay.Text);
                cat.ParentID = M.UrlQuery.parentID;

                C.ProductCategoryController.Insert(cat);
            }
        }

        protected void LinkButtonDelete_Click(object sender, EventArgs e)
        {
            foreach (GridItem gridItem in RadGridMain.SelectedItems)
            {
                GridDataItem dataItem = (GridDataItem)gridItem;
                int id = int.Parse(dataItem["ProductCategoryID"].Text);

                C.ProductCategoryController.Delete(id);
            }
        }

        protected void LinkButtonUpdate_Click(object sender, EventArgs e)
        {
            foreach (GridDataItem item in RadGridMain.Items)
            {
                int id = int.Parse(item["ProductCategoryID"].Text);
                TextBox txtName = item.FindControl("txtName") as TextBox;
                TextBox txtOrderDisplay = item.FindControl("txtOrderDisplay") as TextBox;
                CheckBox chkIsActive = item.FindControl("chkIsActive") as CheckBox;
                ChooseProductCategory ChooseProductCategory1 = (ChooseProductCategory)item.FindControl("ChooseProductCategory1");

                HCDataContext db = new HCDataContext();
                ProductCategory cat = C.ProductCategoryController.GetByID(id, db);
                cat.Name = txtName.Text;
                cat.OrderDisplay = int.Parse(txtOrderDisplay.Text);
                cat.IsActive = chkIsActive.Checked;
                cat.ParentID = null;
                cat.ParentID = ChooseProductCategory1.SelectedCategoryID;

                C.ProductCategoryController.Update(cat, db);
            }
        }

        protected void RadGridMain_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                int id = int.Parse((e.Item as GridDataItem)["ProductCategoryID"].Text);
                ProductCategory cat = C.ProductCategoryController.GetByID(id);

                ChooseProductCategory ChooseProductCategory1 = (ChooseProductCategory)e.Item.FindControl("ChooseProductCategory1");
                ChooseProductCategory1.LoadTreeData();
                ChooseProductCategory1.SelectedCategoryID = cat.ParentID;

                HyperLink childLink = (e.Item as GridDataItem)["ChildLink"].Controls[0] as HyperLink;
                HyperLink propertyLink = (e.Item as GridDataItem)["PropertyLink"].Controls[0] as HyperLink;

                childLink.Text = cat.ProductCategories.Count + " " + childLink.Text;
                propertyLink.Text = cat.ProductProperties.Count + " " + propertyLink.Text;
                if (!C.ProductCategoryController.IsLeafNode(id)) propertyLink.Visible = false;
            }
        }

        protected void btnFilter_Click(object sender, EventArgs e)
        {
            string url = Request.Url.AbsoluteUri;
            url = M.Http.SetPara(url, "ParentID", ChooseProductCategory1.SelectedCategoryID.HasValue ? ChooseProductCategory1.SelectedCategoryID.Value.ToString() : "");
            Response.Redirect(url);
        }
    }
}